﻿using System;
using System.Collections.Generic;

//基类
public abstract class Shape
{
    public abstract double CalculateArea();
}

// 圆形
public class Circle : Shape
{
    private double _radius;

    public double Radius
    {
        get => _radius;
        set => _radius = value > 0 ? value : throw new ArgumentException("半径必须大于0");
    }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }
}

// 矩形
public class Rectangle : Shape
{
    private double _length;
    private double _width;

    public double Length
    {
        get => _length;
        set => _length = value > 0 ? value : throw new ArgumentException("长度必须大于0");
    }

    public double Width
    {
        get => _width;
        set => _width = value > 0 ? value : throw new ArgumentException("宽度必须大于0");
    }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override double CalculateArea()
    {
        return Length * Width;
    }
}

// 正方形
public class Square : Rectangle
{
    private double _side;

    public double Side
    {
        get => _side;
        set
        {
            _side = value;
            base.Length = value;
            base.Width = value;
        }
    }

    public Square(double side) : base(side, side)
    {
        Side = side;
    }
}

// 三角形
public class Triangle : Shape
{
    private double _base;
    private double _height;

    public double Base
    {
        get => _base;
        set => _base = value > 0 ? value : throw new ArgumentException("底边长度必须大于0");
    }

    public double Height
    {
        get => _height;
        set => _height = value > 0 ? value : throw new ArgumentException("高度必须大于0");
    }

    public Triangle(double baseLength, double height)
    {
        Base = baseLength;
        Height = height;
    }

    public override double CalculateArea()
    {
        return 0.5 * Base * Height;
    }
}

// 简单工厂类
public class ShapeFactory
{
    private static Random random = new Random();

    // 创建随机形状
    public static Shape CreateRandomShape()
    {
        // 随机选择形状类型 (0: 圆形, 1: 矩形, 2: 正方形, 3: 三角形)
        int shapeType = random.Next(4);

        switch (shapeType)
        {
            case 0:
                double radius = random.Next(1, 11); // 1-10的随机半径
                return new Circle(radius);
            case 1:
                double length = random.Next(1, 11); // 1-10的随机长度
                double width = random.Next(1, 11); // 1-10的随机宽度
                return new Rectangle(length, width);
            case 2:
                double side = random.Next(1, 11); // 1-10的随机边长
                return new Square(side);
            case 3:
                double baseLength = random.Next(1, 11); // 1-10的随机底边长度
                double height = random.Next(1, 11); // 1-10的随机高度
                return new Triangle(baseLength, height);
            default:
                throw new ArgumentException("无效的形状类型");
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 创建10个随机形状对象
        Shape[] shapes = new Shape[10];
        for (int i = 0; i < shapes.Length; i++)
        {
            shapes[i] = ShapeFactory.CreateRandomShape();
        }

        for (int i = 0; i < shapes.Length; i++)
        {
            double area = shapes[i].CalculateArea();
            Console.WriteLine($"形状 {i + 1}: {shapes[i].GetType().Name}, 面积: {area:F2}");
        }
    }
}
